#!/usr/bin/env python3
import rospy
from std_msgs.msg import String
import requests

rospy.init_node("ros_call_fastapi_node")
pub = rospy.Publisher("/fastapi_response", String, queue_size=10)

def call_fastapi():
    try:
        resp = requests.get("http://127.0.0.1:8000/topics")
        if resp.status_code == 200:
            msg = String()
            msg.data = resp.text
            pub.publish(msg)
            rospy.loginfo(f"Published: {msg.data}")
    except Exception as e:
        rospy.logwarn(f"Failed to call FastAPI: {e}")

if __name__ == "__main__":
    rate = rospy.Rate(1)  # 1Hz 调用
    while not rospy.is_shutdown():
        call_fastapi()
        rate.sleep()
